package com.qf.java2106.huangshengrui.hotel.dao.impl;

import com.qf.java2106.huangshengrui.hotel.dao.IUserDao;
import com.qf.java2106.huangshengrui.hotel.pojos.User;
import com.qf.java2106.huangshengrui.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/28
 **/
public class UserDaoImpl implements IUserDao {

    QueryRunner queryRunner = null;

    @Override
    public Integer userLogin(User loginUser) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT is_admin FROM t_user WHERE username = ? AND password = ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new ScalarHandler<>(),loginUser.getUsername(),loginUser.getPassword());
    }

    @Override
    public User userCheck(String username) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT user_id userId,username,password,is_admin isAdmin FROM t_user WHERE username = ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(User.class),username);
    }

    @Override
    public Integer userLogon(User logonUser) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "INSERT INTO t_user (username,password,is_admin) VALUE (?,?,?)";
        return queryRunner.update(JdbcUtils.getConnection(),sql,logonUser.getUsername(),logonUser.getPassword(),logonUser.getIsAdmin());
    }

}
